package com.factory.test.util;

import java.lang.reflect.Array;
import java.util.Arrays;

public class DataConversion {

	/**
	 * 将byte[]数组转化为String类型
	 * @param arg 需要转换的byte[]数组
	 * @param length 需要转换的数组长度
	 * @return 转换后的String队形
	 */
	public static String toHexString(byte[] arg, int length) {
//		String result = new String();
//		if (arg != null) {
//			for (int i = 0; i < length; i++) {
//				result = result
//						+ (Integer.toHexString(arg[i] < 0 ? arg[i] + 256 : arg[i]).length() == 1 ? "0"
//				+ Integer.toHexString(arg[i] < 0 ? arg[i] + 256 : arg[i]) : Integer
//								.toHexString(arg[i] < 0 ? arg[i] + 256 : arg[i]))
//								+ " ";
//			}
//			return result;
//		}
//		return "";



		if(arg != null) {
			StringBuffer sb = new StringBuffer(length * 2);

			for (int i = 0; i < length; i++) {    // 使用String的format方法进行转换
				sb.append(String.format("%02x", new Integer(arg[i] & 0xff)));
				sb.append(" ");
			}

			return sb.toString();
		}
		return "";
	}

	/**
	 * 将String转化为byte[]数组
	 * @param arg 需要转换的String对象
	 * @return 转换后的byte[]数组
	 */
	public static byte[] toByteArray(String arg) {
		if (arg != null) {
			/* 1.先去除String中的' '，然后将String转换为char数组 */
			char[] NewArray = new char[arg.length()];
			char[] array = arg.toCharArray();
			int length = 0;
			for (int i = 0; i < array.length; i++) {
				if (array[i] != ' ') {
					NewArray[length] = array[i];
					length++;
				}
			}
			/* 将char数组中的值转成一个实际的十进制数组 */
			int EvenLength = (length % 2 == 0) ? length : length + 1;
			if (EvenLength != 0) {
				int[] data = new int[EvenLength];
				data[EvenLength - 1] = 0;
				for (int i = 0; i < length; i++) {
					if (NewArray[i] >= '0' && NewArray[i] <= '9') {
						data[i] = NewArray[i] - '0';
					} else if (NewArray[i] >= 'a' && NewArray[i] <= 'f') {
						data[i] = NewArray[i] - 'a' + 10;
					} else if (NewArray[i] >= 'A' && NewArray[i] <= 'F') {
						data[i] = NewArray[i] - 'A' + 10;
					}
				}
				/* 将 每个char的值每两个组成一个16进制数据 */
				byte[] byteArray = new byte[EvenLength / 2];
				for (int i = 0; i < EvenLength / 2; i++) {
					byteArray[i] = (byte) (data[i * 2] * 16 + data[i * 2 + 1]);
				}
				return byteArray;
			}
		}
		return new byte[] {};
	}
	
	
	/**
	 * 数字字符串转16进制编码
	 * @param s
	 * @return
	 */
	public static String toAsciiString(String s) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < s.length(); i++) {
			int ch = (int) s.charAt(i);
			String s4 = Integer.toHexString(ch);
			sb.append(s4);
			sb.append(' ');
		}
		return sb.toString().trim();
	}




	public static byte[] intToByte4(int i) {
		byte[] targets = new byte[4];
		targets[3] = (byte) (i & 0xFF);
		targets[2] = (byte) (i >> 8 & 0xFF);
		targets[1] = (byte) (i >> 16 & 0xFF);
		targets[0] = (byte) (i >> 24 & 0xFF);
		return targets;
	}

	public static int byteToInt(byte[] b) {
		int mask = 0xff;
		int temp = 0;
		int n = 0;
		for (int i = 0; i < b.length; i++) {
			n <<= 8;
			temp = b[i] & mask;
			n |= temp;
		}
		return n;
	}

	public static void main(String[] args) {
		int i = 204;
		System.out.println(Arrays.toString(intToByte4(i)));
	}


	public static int checkSum(byte[] by){
		int sum = 0;
		for(int i=0; i<by.length; i++){
			sum += by[i] & 0xff;
		}
		return sum;
	}


	/**
	 * 反转数组
	 *
	 */
	public static <T> T invertArray(T array) {
		int len = Array.getLength(array);

		Class<?> classz = array.getClass().getComponentType();

		Object dest = Array.newInstance(classz, len);

		System.arraycopy(array, 0, dest, 0, len);

		Object temp;

		for (int i = 0; i < (len / 2); i++) {
			temp = Array.get(dest, i);
			Array.set(dest, i, Array.get(dest, len - i - 1));
			Array.set(dest, len - i - 1, temp);
		}

		return (T)dest;
	}
}
